package lab_3;

import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.NotFound;

/**
 *
 * @author grp02 - Pascal Charreix and Meike Paetz
 *
 * This is the hotel operator client.
 */
public class HotelOperatorClient {

    private ORB myORB;
    private HotelOperatorContingent contingent;
    private final String contContext = "corbaname:iiop:devdisapp.dynalias.org:900#grp02/contingent";

    /**
     *
     * This is the main
     */
    public static void main(String args[]) {
        HotelOperatorClient client = new HotelOperatorClient(args);
        client.getRemoteContingent();
        client.shutdownORBs();
    }

    /**
     *
     * This is the constructor
     */
    public HotelOperatorClient(String args[]) {
        // Initialize ORB reference
        myORB = ORB.init(args, null);
    }

    /**
     *
     * This is to get the remote contingent
     */
    private void getRemoteContingent() {
        try {
            org.omg.CORBA.Object objRef = myORB.string_to_object(contContext);
            contingent = HotelOperatorContingentHelper.narrow(objRef);
            if (contingent == null) {
                System.out.println("Null account returned.");
                System.exit(1);
            }
            System.out.println("Contingent information :");
            System.out.println("Contingent country : " + contingent.getCountryName());
            System.out.println("Contingent hotel : " + contingent.getHotelName());
            System.out.println("Contingent periode : " + contingent.getTimePeriode());
            System.out.println("Ref : " + objRef);
        } catch (Exception e) {
            System.out.println("Error getting the remote contingent");
            e.printStackTrace();
            System.exit(1);
        }
    }

    /**
     *
     * This is to shut down the ORBs after usage
     */
    private void shutdownORBs() {
        // Necessary to shut down the ORBs, otherwise the client and server
        // will never exit
        System.out.println("Shutting down remote ORBs ...");
        // It is enough to call shutdown() on one object
        contingent.shutdown();
    }
}
